package com.tim.soundseechina;

import org.apache.log4j.Logger;

import com.tim.soundseechina.entity.InformationInfo;

public class Food extends Thread {
	private static Logger log = Logger.getLogger(Food.class);
	private static String MODEL_NAME = "food/";

	public void run() {
		// 特色美食
		String customUrl = SoundSeeChina.URL + "/cn/taste_special.aspx";

		InformationInfo info = new InformationInfo();
		// 特色美食类型
		info.setMenuId(6);

		for (int i = 0; i < 27; i++) {
			String url = customUrl
					+ "?__EVENTTARGET=ctl00$contentMaster$AspNetPager1&__EVENTARGUMENT="
					+ (i + 1);
			String content = getConent(url);
			// 获取特色美食信息
			getPagerInfo(info, content);
			System.out.println(url);
		}
		log.warn("特色美食抓取完成");
	}

	public static void getPagerInfo(InformationInfo info, String content) {
		String temp = StringUtil.getStringFromPattern(content,
				"(.*)<div id=\"special-menu-left2\"");
		String[] tempArr = temp
				.split("<div id=\"special-menu-left1\" style=\"margin-bottom: 20px;\">");
		for (int i = 0; i < tempArr.length; i++) {
			InformationInfo dataInfo = new InformationInfo();
			// 获取信息页面地址
			String infoUrl = StringUtil.getStringFromPattern(tempArr[i],
					"<a href=\"(.*)\" id=");
			// 中文信息页url
			String url = SoundSeeChina.URL + "/cn/" + infoUrl;
			// 获取中文信息
			String tempInfo = getInformationTemp(dataInfo, url);
			// 获取标题
			String title = getInformationTitle(tempInfo);
			// 未抓过-数据入库
			// 设置类型
			dataInfo.setMenuId(info.getMenuId());
			dataInfo.setTypeId(info.getTypeId());
			// 设置中文类型
			dataInfo.setLanType("cn");
			dataInfo.setStatus((short) 0);
			// 设置标题
			dataInfo.setTitle(title);
			// 抓取设置内容
			dataInfo.setInformationDesc(getInformationContent(tempInfo));
			// 获取首个图片地址
			String indexImgStr = StringUtil.getStringFromPattern(tempArr[i],
					"<img src='..(.*)' alt");
			String fileType = indexImgStr.substring(indexImgStr
					.lastIndexOf("."));
			// 图片保存路径
			String imgFile = MODEL_NAME + System.currentTimeMillis() + fileType;
			// 下载图片
			HttpClientProc.getUrl2File(SoundSeeChina.URL + indexImgStr,
					SoundSeeChina.CHINESE_CULTURE + imgFile);
			// 设置首页图片路径
			dataInfo.setIndexImg(imgFile);
			// 中文数据入库
			SoundSeeChina.informationInfoService.create(dataInfo);

			// 英文信息
			InformationInfo enInfo = new InformationInfo();
			// 英文信息页url
			url = SoundSeeChina.URL + "/en/" + infoUrl;
			tempInfo = getInformationTemp(enInfo, url);
			// 获取标题
			title = getInformationTitle(tempInfo);
			// 未抓过-数据入库
			// 设置图片连接
			enInfo.setImgSrc(dataInfo.getImgSrc());
			enInfo.setMenuId(dataInfo.getMenuId());
			enInfo.setTypeId(dataInfo.getTypeId());
			enInfo.setSubTypeStr(dataInfo.getSubTypeStr());
			enInfo.setStatus(dataInfo.getStatus());
			enInfo.setIndexImg(dataInfo.getIndexImg());
			enInfo.setLanType("en");
			enInfo.setTitle(title);
			// 抓取设置内容
			enInfo.setInformationDesc(getInformationContent(tempInfo));
			// 英文数据入库
			SoundSeeChina.informationInfoService.create(enInfo);
		}
	}

	public static String getInformationTemp(InformationInfo info, String url) {
		// 设置来源页
		info.setFromLink(url);
		// 获取页面信息
		String tempStr = HttpClientProc.getUrl2String(url,
				SoundSeeChina.CHAR_SET);
		return StringUtil
				.getStringFromPattern(tempStr,
						"id=\"four-ethnic\">(.*)<div class=\"four-ethnic-bottom-top\">");
	}

	// 获取资讯标题
	public static String getInformationTitle(String content) {
		return StringUtil
				.getStringFromPattern(
						content,
						"<span style=\"color: #0183fd; font-size: 14px; font-weight: bold;\">(.*)</span><br />");
	}

	// 获取资讯内容
	public static String getInformationContent(String content) {
		String temp = StringUtil.getStringFromPattern(content, "<p>(.*)</p>");
		if (temp == null) {
			temp = StringUtil
					.getStringFromPattern(content,
							"(<font face=\"Verdana\" color=\"#808080\" size=\"2\">.*)</p>");
		}
		if (temp == null) {
			temp = StringUtil.getStringFromPattern(content, "(<div><span.*</span></div>)");
		}
		if (temp != null && temp.length() < 10) {
			temp = StringUtil.getStringFromPattern(content, "</span><br />(.*</span></div>)");
		}
		return temp;
	}

	public static String getConent(String url) {
		String content = HttpClientProc.getUrl2String(url,
				SoundSeeChina.CHAR_SET);
		content = StringUtil
				.getStringFromPattern(
						content,
						"<div id=\"special-menu-left1\" style=\"margin-bottom: 20px;\">(.*)<!-- AspNetPager V7.0 for VS2008 End -->");
		return content;
	}
}
